/*package main

import (
	"fmt"
	"github.com/goburrow/modbus"
	"time"
)

const MAXRWLEN = 80

func main() {
	handler := modbus.NewRTUClientHandler("/dev/ttyS1")
	handler.BaudRate = 9600
	handler.DataBits = 8
	handler.Parity = "O"
	handler.StopBits = 1
	handler.SlaveId = 1
	handler.Timeout = 5 * time.Second

	err := handler.Connect()
	defer handler.Close()

	client := modbus.NewClient(handler)
	results, err := client.ReadDiscreteInputs(0, 2)
	fmt.Println("*******************", results, err)

}
*/
/*
   package main

   import (
   	"fmt"
   	"github.com/tarm/serial"
   )

   const MAXRWLEN = 80

   func main() {
   	fmt.Printf("hello world\n")
   	fmt.Println("start")

   	cfg := &serial.Config{Name: "/dev/ttyS1", Baud: 9600, ReadTimeout: 3, Parity: serial.ParityOdd}

   	iorwc, err := serial.OpenPort(cfg)
   	fmt.Println("iorwc", iorwc)
   	if err != nil {
   		fmt.Println("open err", err)
   		return
   	}

   	defer iorwc.Close()
   	buffer := make([]byte, MAXRWLEN)

   	num, err := iorwc.Read(buffer)

   	//num, err = iorwc.Write([]byte{0xfe, 0xfe, 0xfe,0xfe,0x68, 0x79, 0x77,0x13, 0x24, 0x6, 0x20, 0x68, 0x11, 0x4, 0x00, 0x00, 0x01, 0x00, 0x33, 0x16})
   	//num, err = iorwc.Write([]byte{0xfe, 0xfe, 0xfe,0xfe,0x68, 0x79, 0x77,0x13, 0x24, 0x6, 0x20, 0x68, 0x11, 0x4, 0x33, 0x33, 0x34, 0x33, 0xff, 0x16})
   	num, err = iorwc.Write([]byte{0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A})
   	//num, err = iorwc.Write([]byte{0xfe, 0xfe, 0xfe,0xfe,0x68, 0x79, 0x77,0x13, 0x24, 0x6, 0x20, 0x68, 0x11, 0x4, 0x36, 0x34, 0x33, 0x37, 0x6, 0x16})
   	if err != nil {
   		fmt.Println("write err", err)
   		return
   	}

   	var tmpstr string = ""
   	for i := 0; i < 30; i++ {

   		num, err = iorwc.Read(buffer)

   		if num > 0 {
   			fmt.Println("get data ", num)
   			tmpstr += fmt.Sprintf("%x", (buffer[:num]))
   			//break
   		}

   		//查找读到信息的结尾标志
   		//if strings.LastIndex(tmpstr, "\r\nOK\r\n") > 0 {
   		//break
   		//}
   	}

   	//打印输出读到的信息
   	fmt.Println(tmpstr)

   }*/
package main

import (
	"fmt"
	"github.com/AdamSLevy/modbus"
	"time"
)

func main() {
	csRTU := modbus.ConnectionSettings{
		Mode:    modbus.ModeRTU,
		Host:    "/dev/ttyS1",
		Baud:    9600,
		Timeout: 500 * time.Millisecond,
	}
	ch, err := modbus.GetClientHandle(csRTU)
	if nil != err {
		fmt.Println(err)
		return
	}
	readinput, err := modbus.ReadDiscreteInputs(1, 0, 5) // SlaveID, Address, Quantity
	if nil != err {
		fmt.Println(err)
		return
	}
	data, err := ch.Send(readinput)
	fmt.Println("((((((((((", data, err)
}
